// pages/brand/brand.js
const app = getApp();
var pageNo = 1;
var hasNextPage = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brands: null,
    baseDomain: app.globalData.baseDomain
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //调用获取品牌接口
    wx.request({
      url: app.globalData.apiDomain + '/api/brand/getAllBrand/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          brands: res.data.data.list,
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //调用获取品牌接口
    wx.request({
      url: app.globalData.apiDomain + '/api/brand/getAllBrand/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          brands: res.data.data.list,
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
        wx.stopPullDownRefresh();
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    //调用获取品牌列表接口
    if (hasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/brand/getAllBrand/' + pageNo,
        success: function(res) {
          hasNextPage = res.data.data.hasNextPage;
          var oldbrands = that.data.brands;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldbrands.push(list[i]);
          }
          that.setData({
            brands: oldbrands
          });
        },
        complete: function() {
          if (hasNextPage) {
            pageNo++;
          }
        }
      });
    } else {
      wx.showToast({
        title: '没有更多内容了',
        icon: '',
        image: '',
        duration: 1000,
        mask: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})